﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ExportChangesets
{
    public class Logger
    {
        #region Private Members
        public static bool HasEncounteredError = false;
        private static FileInfo _file;
        #endregion

        #region Constants
        public static string LogName = "ExportChangesetLog";
        public static string LogFullPath = string.Format("{0}\\{1}.log",Environment.GetFolderPath(Environment.SpecialFolder.Desktop),Logger.LogName);
        #endregion

        #region Constructors
        public Logger()
        {
            if (!LogFileExists())
            {
                CreateLogFile();
            }
        }
        #endregion

        #region Private Methods
        private bool LogFileExists()
        {
            try
            {
                FileInfo logFile = new FileInfo(Logger.LogFullPath);
                if (logFile.Exists)
                {
                    _file = logFile;
                    return true;
                }
                return false;
            }
            catch
            {
                Console.WriteLine("--> Couldn't check if file exists. Logging will be disabled.");
                return false;
            }
        }

        private void CreateLogFile()
        {
            try
            {
                if (_file == null)
                {
                    FileInfo file = new FileInfo(Logger.LogFullPath);
                    FileStream _fileStream = file.Create();
                    _file = file;
                    _fileStream.Close();
                }
            }
            catch (Exception)
            {
                Console.WriteLine("--> Could not create log file. Logging will be disabled.");
                Logger.HasEncounteredError = true;
            }
        }
        #endregion

        #region Statics
        public static void WriteEntry(string entry)
        {
            if (!string.IsNullOrEmpty(entry) && !Logger.HasEncounteredError)
            {
                try
                {
                    if (_file != null)
                    {
                        using (var writer = _file.AppendText())
                        {
                            writer.WriteLine();
                            writer.WriteLine("-----------------------------");
                            writer.WriteLine(string.Format("{0}", DateTime.Now));
                            writer.WriteLine(entry);
                            writer.WriteLine("-----------------------------");
                            writer.WriteLine();
                        }
                    }
                }
                catch
                {
                   Console.WriteLine("--> Error writing to log file. Logging will be disabled.");
                   Logger.HasEncounteredError = true;
                }
            }
        }
        #endregion
    }
}
